package com.redlight.base;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by chao on 2019/1/9.
 */
public class DateUtil {
    private static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public static final String[] parsePatterns = new String[]{"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
    public static final SimpleDateFormat df;
    public static final SimpleDateFormat dtf;

    public DateUtil() {
    }

    public static final int currentYear() {
        return Calendar.getInstance().get(1);
    }

    public static int currentMonth() {
        return Calendar.getInstance().get(2) + 1;
    }

    public static String getSysDate() {
        return formatDate(new Date(), parsePatterns[0]);
    }

    public static String getSysDate(Object fmt) {
        return formatDate(new Date(), fmt);
    }

    public static String getSysDateTime() {
        return formatDate(new Date(), parsePatterns[1]);
    }

    public static String formatDate(Date d, Object format) {
        if(null == d) {
            return null;
        } else {
            try {
                SimpleDateFormat formatter = new SimpleDateFormat(format.toString());
                return formatter.format(d);
            } catch (Exception var3) {
                return null;
            }
        }
    }

    public static Date parseDate(String str) {
        Date sDate = null;

        try {
            sDate = df.parse(str);
            return sDate;
        } catch (Exception var3) {
            return null;
        }
    }

    public static Date parseformatDate(Object d, String format) {
        if(null == d) {
            return null;
        } else {
            try {
                SimpleDateFormat formatter = new SimpleDateFormat(format);
                String dStr = null;
                if(d instanceof Date) {
                    dStr = formatter.format(d);
                } else {
                    dStr = d.toString();
                }

                return formatter.parse(dStr);
            } catch (Exception var4) {
                var4.printStackTrace();
                return null;
            }
        }
    }

    public static String formatDateTime(long timeMillis) {
        long day = timeMillis / 86400000L;
        long hour = timeMillis / 3600000L - day * 24L;
        long min = timeMillis / 60000L - day * 24L * 60L - hour * 60L;
        long s = timeMillis / 1000L - day * 24L * 60L * 60L - hour * 60L * 60L - min * 60L;
        long sss = timeMillis - day * 24L * 60L * 60L * 1000L - hour * 60L * 60L * 1000L - min * 60L * 1000L - s * 1000L;
        return (day > 0L?day + ",":"") + hour + ":" + min + ":" + s + "." + sss;
    }

    public static String formatDateTime(Date date) {
        return formatDate(date, "yyyy-MM-dd HH:mm:ss");
    }

    public static String getTimeDiff(Date d) {
        String ret = "";
        if(null == d) {
            return "";
        } else {
            Date d1 = new Date();
            long num = (long)differYears(d1, d);
            if(num <= 0L) {
                num = differDays(d1, d);
                if(num <= 0L) {
                    num = differHour(d1, d);
                    if(num <= 0L) {
                        num = differMins(d1, d);
                        if(num <= 0L) {
                            ret = formatDateTime(d);
                        } else {
                            ret = num + "分钟";
                        }
                    } else {
                        ret = num + "小时";
                    }
                } else {
                    ret = num + "天";
                }
            } else {
                ret = num + "年";
            }

            return ret;
        }
    }

    public static long pastMinutes(Date date) {
        long t = (new Date()).getTime() - date.getTime();
        return t / 60000L;
    }

    public static long differMins(Date dateTime1, Date dateTime2) {
        if(null != dateTime1 && null != dateTime2) {
            long t = dateTime1.getTime() - dateTime2.getTime();
            return t / 60000L;
        } else {
            return -1L;
        }
    }

    public static long differHour(Date dateTime1, Date dateTime2) {
        long t = dateTime1.getTime() - dateTime2.getTime();
        return t / 3600000L;
    }

    public static long differDays(Object dateBegin, Object dateEnd) {
        Date d1 = null;
        Date d2 = null;
        if(dateBegin instanceof Date) {
            d1 = (Date)dateBegin;
        } else {
            d1 = parseDate(dateBegin.toString());
        }

        if(dateEnd instanceof Date) {
            d2 = (Date)dateEnd;
        } else {
            d2 = parseDate(dateEnd.toString());
        }

        return (d2.getTime() - d1.getTime()) / 86400000L;
    }

    public static int differYears(Object dateBegin, Object dateEnd) {
        Date date1 = null;
        Date date2 = null;
        if(dateBegin instanceof Date) {
            date1 = (Date)dateBegin;
        } else {
            date1 = parseDate(dateBegin.toString());
        }

        if(dateEnd instanceof Date) {
            date2 = (Date)dateEnd;
        } else {
            date2 = parseDate(dateEnd.toString());
        }

        Calendar c1 = Calendar.getInstance();
        c1.setTime(date1);
        Calendar c2 = Calendar.getInstance();
        c2.setTime(date2);
        int d1 = c1.get(5);
        int m1 = c1.get(2) + 1;
        int y1 = c1.get(1);
        int d2 = c2.get(5);
        int m2 = c2.get(2) + 1;
        int y2 = c2.get(1);
        if(d2 < d1) {
            --m2;
        }

        if(m2 < m1) {
            --y2;
        }

        int retY = y2 - y1;
        return retY < 0?0:retY;
    }

    public static boolean before(Date d1, Date d2, boolean eqFlag) {
        try {
            String dateStr1 = formatDate(d1, parsePatterns[0]);
            String dateStr2 = formatDate(d2, parsePatterns[0]);
            long n = differDays(dateStr1, dateStr2);
            if(n > 0L) {
                return true;
            } else if(eqFlag && 0L == n) {
                return true;
            } else {
                d1 = df.parse(dateStr1);
                d2 = df.parse(dateStr2);
                return d2.after(d1);
            }
        } catch (Exception var7) {
            var7.printStackTrace();
            return false;
        }
    }

    public static Date dateAddDay(Date oldDate, int n) {
        if(oldDate == null) {
            return null;
        } else {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(oldDate);
            calendar.add(5, n);
            return calendar.getTime();
        }
    }

    public static Date dateAddMonth(Date oldDate, int n) {
        if(oldDate == null) {
            return null;
        } else {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(oldDate);
            calendar.add(2, n);
            return calendar.getTime();
        }
    }

    public static Date dateFirstDay(Date oldDate) {
        Calendar c = Calendar.getInstance();
        c.setTime(oldDate);
        c.add(2, 0);
        c.set(5, 1);
        return c.getTime();
    }

    public static Date dateLastDay(Date oldDate) {
        Calendar c = Calendar.getInstance();
        c.setTime(oldDate);
        c.set(5, c.getActualMaximum(5));
        return c.getTime();
    }

    public static String monthLastday(int year, int month) {
        Calendar ca = Calendar.getInstance();
        ca.set(1, year);
        ca.set(2, month - 1);
        ca.set(5, 1);
        ca.set(5, ca.getActualMaximum(5));
        return df.format(ca.getTime());
    }

    public static Date weekFirstDay(int year, int month, int week) {
        Calendar cal = Calendar.getInstance();
        cal.set(1, year);
        cal.set(2, month);
        cal.set(4, week);
        cal.set(7, 2);
        return cal.getTime();
    }

    public static Date weekLastDay(int year, int month, int week) {
        Calendar cal = Calendar.getInstance();
        cal.set(1, year);
        cal.set(2, month);
        cal.set(4, week);
        cal.set(7, 1);
        return cal.getTime();
    }

    public static int getWeekOfDate(Date dt) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        return cal.get(7) - 1;
    }

    public static long string2long(String sdate) {
        if(sdate.length() < 11) {
            sdate = sdate + " 00:00:00";
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dt2 = null;

        try {
            dt2 = sdf.parse(sdate);
        } catch (ParseException var5) {
            var5.printStackTrace();
        }

        long lTime = dt2.getTime() / 1000L;
        return lTime;
    }

    public static String long2string(long ldate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dt = new Date(ldate * 1000L);
        String sDateTime = sdf.format(dt);
        if(sDateTime.endsWith("00:00:00")) {
            sDateTime = sDateTime.substring(0, 10);
        }

        return sDateTime;
    }

    static {
        df = new SimpleDateFormat(parsePatterns[0]);
        dtf = new SimpleDateFormat(parsePatterns[1]);
    }
}
